package com.tms.springboot.controller;


import com.tms.springboot.feign.TaskPickupDispatchFeign;
import com.tms.springboot.oms.pojo.PdOrder;
import com.tms.springboot.result.JsonResult;
import com.tms.springboot.result.ResultCode;
import com.tms.springboot.result.ResultTool;
import com.tms.springboot.service.PdOrderCargoService;
import com.tms.springboot.service.PdOrderService;
import com.tms.springboot.utils.UUId;
import com.tms.springboot.vo.PdOrderTypeVo;
import com.tms.springboot.work.pojo.PdTaskPickupDispatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author cjj
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-order")
public class PdOrderController {


    @Autowired
    PdOrderService pdOrderService;

    @Autowired
    PdOrderCargoService pdOrderCargoService;

    @Autowired
    TaskPickupDispatchFeign taskPickupDispatchFeign;
    //用户添加订单
    @RequestMapping("/addorder")
    public JsonResult addOrder(@RequestBody PdOrderTypeVo pdOrderTypeVo) {
        String uuid = UUId.nextId();
        pdOrderTypeVo.getPdOrder().setId(uuid);
        boolean sum = pdOrderService.save(pdOrderTypeVo.getPdOrder());
        if (sum) {
            pdOrderTypeVo.getPdOrderCargo().setOrderId(uuid);
            boolean op = pdOrderCargoService.save(pdOrderTypeVo.getPdOrderCargo());
            if (op) {
                if(pdOrderTypeVo.getEstimatedStartTime()!=null){
                    PdTaskPickupDispatch pdTaskPickupDispatch =new PdTaskPickupDispatch();
                    pdTaskPickupDispatch.setOrderId(uuid);
                    pdTaskPickupDispatch.setTaskType(1);
                    pdTaskPickupDispatch.setStatus(1);
                    pdTaskPickupDispatch.setAssignedStatus(1);
                    pdTaskPickupDispatch.setCreateTime(pdOrderTypeVo.getPdOrder().getCreateTime());
                    pdTaskPickupDispatch.setEstimatedStartTime(pdOrderTypeVo.getEstimatedStartTime());
                    taskPickupDispatchFeign.addPdTask(pdTaskPickupDispatch);
                }
                return ResultTool.success(ResultCode.SUCCESS);
            } else {
                return ResultTool.fail(ResultCode.COMMON_FAIL);
            }
        }
        return ResultTool.fail(ResultCode.COMMON_FAIL);
    }

    //用户修改订单状态:确认收货
    @RequestMapping("/updateOrder")
    public JsonResult updateOrder(@RequestBody PdOrder pdOrder) {

        boolean sum = pdOrderService.updateById(pdOrder);
        if (sum) {
            return ResultTool.success(ResultCode.SUCCESS);
        }
        return ResultTool.fail(ResultCode.COMMON_FAIL);
    }

    //用户修改订单状态:取消订单
    @GetMapping("/cancelOrder")
    public JsonResult cancelOrder(String id) {

        PdOrder pdOrder = new PdOrder();
        pdOrder.setId(id);
        pdOrder.setStatus(11);
        boolean sum = pdOrderService.updateById(pdOrder);
        if (sum) {
            return ResultTool.success(ResultCode.SUCCESS);
        }
        return ResultTool.fail(ResultCode.COMMON_FAIL);
    }
}
